from flaskapp.util.Sqlalchemy import db

class Maintain(db.Model):
    __tablename__ = 'maintenance_request'

    request_id = db.Column(db.Integer, primary_key=True)
    tenant_id = db.Column(db.Integer, db.ForeignKey('user.user_id'), nullable=False)
    landlord_id = db.Column(db.Integer, db.ForeignKey('user.user_id'), nullable=False)
    house_id = db.Column(db.Integer, db.ForeignKey('house.house_id'), nullable=False)
    content = db.Column(db.Text, nullable=False)
    submitted_at = db.Column(db.DateTime, default=db.func.current_timestamp())
    status = db.Column(db.Enum('待处理', '处理中', '已完成', '已取消'), default='待处理')
    processed_at = db.Column(db.DateTime)
    result_notes = db.Column(db.Text)

    def __init__(self, tenant_id, landlord_id, house_id, content):
        self.tenant_id = tenant_id
        self.landlord_id = landlord_id
        self.house_id = house_id
        self.content = content

    def to_dict(self):
        return {
            'request_id': self.request_id,
            'tenant_id': self.tenant_id,
            'landlord_id': self.landlord_id,
            'house_id': self.house_id,
            'content': self.content,
            'submitted_at': self.submitted_at.isoformat() if self.submitted_at else None,
            'status': self.status,
            'processed_at': self.processed_at.isoformat() if self.processed_at else None,
            'result_notes': self.result_notes
        }